/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package hello;

import java.io.DataInputStream;

import java.io.IOException;

import javax.bluetooth.DiscoveryAgent;
import javax.bluetooth.LocalDevice;

import javax.bluetooth.UUID;
import javax.microedition.io.Connector;
import javax.microedition.io.StreamConnection;
import javax.microedition.io.StreamConnectionNotifier;
import javax.microedition.lcdui.Alert;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.TextBox;
import javax.microedition.lcdui.TextField;
import javax.microedition.midlet.MIDlet;
import javax.microedition.midlet.MIDletStateChangeException;

/**
 *
 * @author leonaaaardo
 */
public class ServerBT extends MIDlet implements Runnable, CommandListener{

    private StreamConnectionNotifier notifier;
   public static final UUID SERVICIO_CHAT = new UUID(0x2345);
    private Command comenzar;
    private Form principal;
    private Thread thread;
    private Display display;
    private LocalDevice localDevice;
    private TextBox texto;
    private StreamConnection connection = null;
    private DataInputStream in = null;
    public static final int atributoSaludo = 0x1111;
    protected void startApp() throws MIDletStateChangeException {
        thread = new Thread(this);
         texto = new TextBox("Mensaje",
"", 50, TextField.UNEDITABLE);
        comenzar = new Command("Comenzar",Command.ITEM,1);
        principal = new Form("Hola Mundo Bluetooth");
        principal.addCommand(comenzar);
        principal.setCommandListener(this);
        display = Display.getDisplay(this);
        display.setCurrent( principal);

    }

    protected void pauseApp() {

    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {

    }

  
    public void commandAction(Command c, Displayable d) {
        if(c.getLabel().equalsIgnoreCase("Comenzar"))
        {
            this.thread.start();

        }
    }

    public void run() {
try {
LocalDevice localDevice =
LocalDevice.getLocalDevice();
if(!localDevice.setDiscoverable(
DiscoveryAgent.GIAC)) {
Display.getDisplay(this).setCurrent(
new Alert("Imposible ofrecer"+
" un servicio"));
}
StringBuffer url =
new StringBuffer("btspp://localhost:");
url.append(SERVICIO_CHAT.toString());
url.append(";name=Servicio chat;authorize=false");
notifier =
(StreamConnectionNotifier)
Connector.open(url.toString());
principal.append("Servidor en marcha...");



while(true) {
receive();
}
} catch(IOException e) {
e.printStackTrace();
Display.getDisplay(this).setCurrent(
new Alert("Error: "+e));
}
}

private synchronized void receive()
{
    try {
connection =
notifier.acceptAndOpen();
in =
connection.openDataInputStream();

String s = in.readUTF();

texto.setString( "/"+s);
Display.getDisplay(this).setCurrent(texto);

} catch(IOException e) {
e.printStackTrace();
} finally {
try {
if(in != null)
in.close();

if(connection != null)
connection.close();
} catch(IOException e) {}
}

}

}
